package io.renren.service.business;

import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.utils.PageUtils;
import io.renren.dto.ArticleEvaluateDto;
import io.renren.dto.ArticleListDto;
import io.renren.entity.Article;
import io.renren.entity.ArticleComment;
import io.renren.service.ArticleCommentService;
import io.renren.service.ArticleService;
import io.renren.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author winson
 * @CREATE 11 23:56
 */
@Service("articleBusinessService")
public class ArticleBusinessService {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCommentService articleCommentService;

    @Autowired
    private SysUserService sysUserService;

    public List<ArticleListDto> getArticleList( Integer page ){
        Page<ArticleListDto> data = articleService.queryArticlePage( page, 8 );

        List<ArticleListDto> list = data.getRecords();
        if( list == null ){
            return  new ArrayList<>();
        }

        for( ArticleListDto dto : list ){
            dto.setEvaluateCount( articleCommentService.queryByArticle( dto.getArticleId() ) );
        }
        return list;

    }

    public Article queryArticleDetail( Integer articleId){
        return articleService.queryDetail( articleId );
    }

    public void evaluate( Integer userId, Integer articleId, String content ){
        ArticleComment articleComment = new ArticleComment();
        articleComment.setArticleId( articleId);
        articleComment.setAuthorId( userId );
        articleComment.setContent( content );
        articleCommentService.insert( articleComment );
    }


    public List<ArticleEvaluateDto> getArticleCommentList( int page ){
        Map<String,Object> param = new HashMap<>();
        param.put("page", page +"");
        param.put("limit", "10");
        PageUtils pageUtils = articleCommentService.queryPage( param );
        List<ArticleComment> commentList = (List<ArticleComment>)pageUtils.getList();
        if( commentList == null ){
            return new ArrayList<>();
        }

        List<ArticleEvaluateDto> data = new ArrayList<>( commentList.size() );
        for( ArticleComment comment : commentList ){
            ArticleEvaluateDto dto = new ArticleEvaluateDto();
            dto.setContent( comment.getContent() );
            dto.setCreateTime( comment.getCreateTime() );
            dto.setAuthorName( sysUserService.queryNickName( comment.getAuthorId() ) );
            data.add( dto );
        }

        return data;
    }


}
